import { message } from 'ant-design-vue';
import axios, { type AxiosRequestConfig } from 'axios';
import type { IBaseRequest, IPageRequest } from '@/types/request';
export const BASE_URL = 'http://localhost:8081';
const request = axios.create({ baseURL: `${BASE_URL}/api/admin/v1` });

// 请求拦截器
request.interceptors.request.use((config) => {
  const { token } = useAuth();
  if (token.value) {
    config.headers.Authorization = token.value;
  }
  return config;
});

// 响应拦截器
request.interceptors.response.use((response) => {
  
  const data = response.data;

  if (data.code !== 0) {
    message.error(data.msg);
    
  }
  return response;
});

export default async function <T = null, P extends boolean = false>(
   url: string,
  options?: AxiosRequestConfig){
  return (
    await request({
      url,
      ...options,
    })
  ).data as P extends true ? IPageRequest<T> : IBaseRequest<T>;
}

